#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
//ESP8266 NODEMCU

const char *ssid     = "LAPTOP9#337";
const char *password = "xy1229033519";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp1.aliyun.com",60*60*8, 30*60*1000);
void setup()
{
 
  Serial.begin(115200);
  wifiInit();
  timeClient.begin();
}

String time1="";
void loop()
{
  timeClient.update();
  Serial.print("[info] today is :");
  Serial.println(timeClient.getDay());
  time1=timeClient.getFormattedTime();
  Serial.print("[info] time is :");
  Serial.println(time1);
  String part01 = getValue(time1,':',0);
  String part02 = getValue(time1,':',1);

  delay(1000); 
    
}

void wifiInit(){
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }
}


// 切割字符串
String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }
  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
